<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use App\Common\Constants\ErrorCode;
use App\CustHub\Service\CommonService;
use App\Enum\WordTypeEnum;
use App\Exception\BusinessException;
use Mine\MineModel;

/**
 * @property int $id 
 * @property int $uid 
 * @property int $main_uid 
 * @property int $cs_id 客服ID
 * @property string $title 
 * @property string $keyword 关键词
 * @property int $match_type 匹配方式：0=精确匹配 1=模糊匹配
 * @property int $is_personal 1个人 0企业
 * @property int $group_id 0为默认
 * @property int $sort 
 * @property string $msg_types 
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property int $main_sort 
 * @property-read mixed $content 
 * @property-read null|ChWordGroup $group 
 * @property-read mixed $msg_types_text 
 */
class ChWord extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_word';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'uid', 'main_uid', 'cs_id', 'title', 'keyword', 'match_type', 'content', 'is_personal', 'group_id', 'sort', 'msg_types', 'created_at', 'updated_at', 'main_sort'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'uid' => 'integer', 'main_uid' => 'integer', 'cs_id' => 'integer', 'match_type' => 'integer', 'content' => 'array', 'is_personal' => 'integer', 'group_id' => 'integer', 'sort' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'main_sort' => 'integer'];


    protected array $appends = ['msg_types_text'];

    public function getContentAttribute()
    {
        return !empty($this->attributes['content'])?json_decode($this->attributes['content'],true):[];
    }

    public function group()
    {
        return $this -> hasOne(ChWordGroup::class,'id','group_id');
    }

    public function getMsgTypesTextAttribute()
    {
        $typeText = '';
        $typesArray = WordTypeEnum::getMap();
        $msgTypes = !empty($this->attributes['msg_types'])?explode(",",$this->attributes['msg_types']):[];
        if(!empty($msgTypes)){
            if(count($msgTypes) > 1){
                $typeText = "复合消息";
            }else{
                $typeText = $typesArray[$msgTypes[0]];
            }
        }
        return $typeText;
    }

    public static function  saveWord($uid,$mainUid,$data = [],$id = 0)
    {

        $model = new self();
        if($id){
            $model = self::where('id',$id) -> first();
            if(empty($model)) throw new BusinessException(ErrorCode::NOT_ISSET,'数据不存在');
        }else{
            $model -> sort = self::query() -> max('sort') + 1;
        }
        $model -> uid = $uid;
        $model -> title = $data['title'];
        $model -> group_id = !empty($data['group_id'])?$data['group_id']:0;
        if(!is_array($data['content'])){
            $data['content'] = json_decode($data['content'],true);
        }
        $info = CommonService::formatMsgContent($data['content']);
        $model -> content = $info['content'];
        $model -> msg_types = $info['msg_types'];
        $model -> is_personal = $data['is_personal'];
        $model -> main_uid = $mainUid;
        $model -> save();
        return $model;
    }

}
